//
//  NewsItemViewController.m
//  KG
//
//  Created by Eugene Braginets on 24.03.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NewsItemViewController.h"
#import "AppDelegate.h"
#import "NewsItem.h"
#import "bannerView.h"

@implementation NewsItemViewController
@synthesize activityIndicator=_activityIndicator;
@synthesize webView=_webView;
@synthesize bannerViewController = _bannerViewController;
@synthesize itemNews=_itemNews;
@synthesize loaded=_loaded;
@synthesize itemNewsName=_itemNewsName;
@synthesize timer=_timer;

- (id)initWithItemNews:(NSString *)itemNewsPassed {
    self = [super initWithNibName:@"NewsItemViewController" bundle:nil];
    if (self) {
        
        self.itemNewsName=itemNewsPassed;
        NSLog (@"itemNewsPassed=%@",self.itemNewsName);

        
    }
    return self;
}

- (void) updateUI {
    [self.webView loadHTMLString:[NSString stringWithFormat:@"<font face=\"arial\"/><img width='%f' src='%@'/><br></br>%@",self.view.frame.size.width-18, self.itemNews.imageAddr,self.itemNews.body] baseURL:[NSURL URLWithString:@""]];
}

- (void) reload:(NSNotification*) notify {
    NSLog (@"reload event");
    
//        [[NSNotificationCenter defaultCenter] 
//         removeObserver:self
//         name:self.itemNewsName
//         object:nil];

    self.itemNews= [[[myAppDelegate dataArraysStore] objectForKey:self.itemNewsName] objectAtIndex:0];
    

    NSLog (@"LoadHTMl event");
    if (self) {
        [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
       
    }
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) { 
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.webView stopLoading];
    self.webView.delegate = nil;
}

- (void) segmentAction:(id)sender {
    
}

- (void)viewDidLoad {
 

    
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwypedLeft:)];
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeLeft setDelaysTouchesBegan:YES];
    [[self view] addGestureRecognizer:swipeLeft];
    
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwypedRight:)];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [swipeRight setDelaysTouchesBegan:YES];
    [[self view] addGestureRecognizer:swipeRight];
    
    
    
//    NSArray *segItemsArray = [NSArray arrayWithObjects: @"<", @">", nil];
//    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray];
//    segmentedControl.frame = CGRectMake(250, 0, 70, 30);
//    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
//    segmentedControl.selectedSegmentIndex = 2;
//    
//    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//    [self.navigationItem setTitleView:segmentedControl];

    
    
//    UIImageView* bt1=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fcheckin"]];
//    
//    UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] initWithCustomView:bt1];
//                                     

//    UIBarButtonItem* logoutButton=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fcheck2"] style:UIBarButtonItemStyleBordered target:self action:@selector(checkIn)];
//    
//    UIBarButtonItem* logoutButton=[[UIBarButtonItem alloc] initWithTitle:@"Like" style:UIBarButtonItemStyleBordered target:self action:@selector(checkIn)];
//    [logoutButton setTintColor:[UIColor colorWithRed:0.16 green:0.16 blue:0.5 alpha:1]];
//
//    [logoutButton setTitle:@"www"];
    //UIBarButtonItemStyleBordered target:self action:@selector(checkIn)];

    
//    UIImage *chatImage = [UIImage imageNamed:@"fcheck2"];
//    
//    UIButton *chatButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [chatButton setBackgroundImage:chatImage forState:UIControlStateNormal];
//    [chatButton setTitle:@"CheckIn" forState:UIControlStateNormal];
//    chatButton.frame = (CGRect) {
//        .size.width = 100,
//        .size.height = 30,
//    };
//    
//    UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:chatButton];
//    self.navigationItem.rightBarButtonItem=barButton;
//    
    
    
//    self.navigationItem.rightBarButtonItem = logoutButton;
    [self.view setBackgroundColor:[UIColor colorWithRed:0.23 green:0.35 blue:0.60 alpha:1]];
    [self.webView setHidden:YES];
    [self.webView setBackgroundColor:[UIColor clearColor]];
    [self.webView setOpaque:NO];
    
    [self.activityIndicator setHidden:NO];
    [self.activityIndicator startAnimating];

    
    
    if ([[myAppDelegate dataArraysStore] objectForKey:self.itemNewsName]!=nil) {
        
        [self reload:nil];   
        
    }
    else {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector: @selector(reload:)
                                                     name:self.itemNewsName 
                                                   object: nil];
    
    }
    
    bannerView *bannerVC=[[bannerView alloc] init];
    [self.view addSubview:bannerVC.view];
    [bannerVC animate];
    self.bannerViewController=bannerVC;
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload {
    
    
        [[NSNotificationCenter defaultCenter] 
         removeObserver:self
         name:self.itemNewsName
         object:nil];

    [self.webView stopLoading];
    
    
    [self setWebView:nil];
    [self setActivityIndicator:nil];
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

#pragma mark - WebView Delegate Methods

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
   
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.activityIndicator setHidden:YES];
   
    //  self.careerWebView.frame = CGRectMake(320, 10, 315, 350);
    self.webView.hidden = NO;
    self.webView.alpha=0;
    [UIView animateWithDuration:0.4 animations:^{
        //        self.careerWebView.frame = CGRectMake(0, 10, 315, 350);
        self.webView.alpha=1;
    }];
    
}

#pragma mark - Social Plugins
- (IBAction)likeButton:(id)sender {
    
    [[myAppDelegate facebook] requestWithGraphPath:@"127747817250603_353729301319119/likes/likes" 
                                         andParams:[NSMutableDictionary dictionaryWithObject:@"" forKey:@"message"]
                                     andHttpMethod:@"POST"
                                       andDelegate:myAppDelegate];
}


#pragma mark - other View Controller Methods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



- (IBAction)didSwypedLeft:(UISwipeGestureRecognizer *)sender {
    NSLog(@"swyped left!");
    
//    [[[myAppDelegate dataArraysStore] objectForKey:self.itemNewsName] objectAtIndex:0] ;
//    
//    NewsItemViewController* vc=[[NewsItemViewController alloc] initWithItemNews:self.itemNewsName];
//    [self.navigationController pushViewController:vc animated:NO];
//    [self.navigationController popViewControllerAnimated:YES];
//    
}
- (IBAction)didSwypedRight:(UISwipeGestureRecognizer *)sender {
    NSLog(@"swyped right!");
}

@end
